using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace eBirdAttend
{
    public partial class Company : UserControl
    {
        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();
        String strBtnCaption = "";
        public Company()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
            ToolTip _toolTip = new ToolTip();
            _toolTip.SetToolTip(btnEdit, "Edit the selected Company Details");
        }

        // Function name   : btnEdit_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : Priya Purushothaman
        // Modified On     : 10/22/2007
        // Purpose         : To check the row content on edit
        // Argument        : object sender
        // Argument        : EventArgs e        
        private void btnEdit_Click(object sender, EventArgs e)
        {
            ClearControls();
            if (btnEdit.Text == "ADD")
            {
                groupBox1.Visible = true;
                strBtnCaption = "Add";
                groupBox1.Text = "ADD COMPANY";               

                //to get the max company id from from the db
                try
                {
                    sqlCmd.CommandText = "select max(CmpId) from company";
                    sqlCmd.Connection = sqlCon;
                    sqlCon.Open();
                    SqlDataReader sqlReader;
                    sqlReader = sqlCmd.ExecuteReader();
                    if (sqlReader.Read())
                    {
                        if (sqlReader[0].ToString() == "")
                        {
                        }
                        else
                        {
                            txtCompId.Text = Convert.ToString(Convert.ToInt32(sqlReader[0].ToString()) + 1);
                        }
                    }
                    else
                    {
                        txtCompId.Text = "1";
                    }

                    if (txtCompId.Text == "")
                    {
                        txtCompId.Text = "1";
                    }
                    txtCompName.Focus();
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message.ToString());
                }
                finally
                {
                    sqlCon.Close();
                }
            }
            else
            {
                if (companyView.SelectedRows.Count == 0)
                { MessageBox.Show("Please select a row for edit"); return; }

                if ((companyView.RowCount - 1) == companyView.SelectedRows[0].Index)
                {
                    MessageBox.Show("No content to edit");
                    groupBox1.Visible = false;
                    btnEdit.Visible = true;
                    ClearControls();
                    return;
                }

                groupBox1.Visible = true;
                strBtnCaption = "Edit";
                groupBox1.Text = "EDIT COMPANY";

                btnSave.Visible = true;
                txtCompName.Focus();    

                //MessageBox.Show(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
                txtCompId.Text = companyView.SelectedRows[0].Cells[0].Value.ToString();
                txtCompName.Text = companyView.SelectedRows[0].Cells[1].Value.ToString();
                txtCompAddress.Text = companyView.SelectedRows[0].Cells[2].Value.ToString();
                txtCompCity.Text = companyView.SelectedRows[0].Cells[3].Value.ToString();
                txtCompState.Text = companyView.SelectedRows[0].Cells[4].Value.ToString();
                txtCompPhone.Text = companyView.SelectedRows[0].Cells[5].Value.ToString();
                txtCompWebSite.Text = companyView.SelectedRows[0].Cells[6].Value.ToString();
                txtCompFax.Text = companyView.SelectedRows[0].Cells[7].Value.ToString();
            }
            
        }

        private void Company_Load(object sender, EventArgs e)
        {



            companyTableAdapter.Connection.ConnectionString = Properties.Settings.Default["ConnectionString"].ToString(); ;
            companyTableAdapter.Fill(companyDataSet1.Company);
            if (companyView.RowCount == 1)
            {
                btnEdit.Text = "ADD";
            }
            else
            {
                btnEdit.Text = "EDIT";
            }
            companyView.Columns[0].Visible = false;
        }

        // Function name   : btnSave_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateControls())
            {
                //save the new company details
                try
                {
                    if (strBtnCaption == "Add")
                    {
                        sqlCmd.CommandText = "Insert into Company values('" + txtCompName.Text + "','" + txtCompAddress.Text + "','"
                                                 + txtCompCity.Text + "','" + txtCompState.Text + "','" + txtCompPhone.Text + "','"
                                                 + txtCompWebSite.Text + "','" + txtCompFax.Text + "')";
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();
                        companyTableAdapter.Fill(companyDataSet1.Company);
                        MessageBox.Show("Successfuly Inserted");
                        btnEdit.Text = "EDIT";
                    }
                    else
                    {
                        //update the company details
                        while (companyView.Rows.Count > 1)
                            companyView.Rows.RemoveAt(0);

                        sqlCmd.CommandText = "update  Company set CmpName='" + txtCompName.Text + "',CmpAddress='" + txtCompAddress.Text + "',CmpCity='" + txtCompCity.Text + "' ,CmpState='" + txtCompState.Text + "',CmpPhone='" + txtCompPhone.Text + "' ,CmpWebsite='" + txtCompWebSite.Text + "',CmpFax='" + txtCompFax.Text + "'   where cmpId='" + txtCompId.Text + "'";
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();
                        sqlCon.Close();

                        companyTableAdapter.Fill(companyDataSet1.Company);

                        MessageBox.Show("Successfuly Updated");
                    }
                }
                catch (Exception e1)
                {
                }
                finally
                {
                    sqlCon.Close();
                    groupBox1.Visible = false;
                    ClearControls();
                }
            }
        }

        // Function name   : btnAdd_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        //private void btnAdd_Click(object sender, EventArgs e)
        //{            
        //    groupBox1.Visible = true;
        //    strBtnCaption = "Add";
        //    groupBox1.Text = "ADD A COMPANY";
        //    ClearControls();

        //    //to get the max company id from from the db
        //    try
        //    {
        //        sqlCmd.CommandText = "select max(CmpId) from company";
        //        sqlCmd.Connection = sqlCon;
        //        sqlCon.Open();
        //        SqlDataReader sqlReader;
        //        sqlReader = sqlCmd.ExecuteReader();
        //        if (sqlReader.Read())
        //        {
        //            if (sqlReader[0].ToString() == "")
        //            {
        //            }
        //            else
        //            {
        //                txtCompId.Text = Convert.ToString(Convert.ToInt32(sqlReader[0].ToString()) + 1);
        //            }
        //        }
        //        else
        //        {
        //            txtCompId.Text = "1";
        //        }

        //        if (txtCompId.Text == "")
        //        {
        //            txtCompId.Text = "1";
        //        }
        //        txtCompName.Focus();
        //    }
        //    catch (Exception e1)
        //    {
        //        MessageBox.Show(e1.Message.ToString());
        //    }
        //    finally
        //    {
        //        sqlCon.Close();
        //    }
        
            
        //}


        // Function name   : btnCancel_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            btnEdit.Visible = true;
            ClearControls();
        }



        // Function name   : ValidateControls
        // Description     : to validate the contorls before inserting into the database
        // Return type     : bool 
        // Author          : Priya Purushothaman
        // Date            : 10/18/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        public bool ValidateControls()
        {
            if (txtCompName.Text == "")
            {
                MessageBox.Show("Enter the company name");
                txtCompName.Focus();
                return false;
            }
            else if (txtCompCity.Text == "")
            {
                MessageBox.Show("Enter the city");
                txtCompCity.Focus();
                return false;
            }
            else if (txtCompPhone.Text == "")
            {
                MessageBox.Show("Enter the phone");
                txtCompPhone.Focus();
                return false;
            }
            else
            {
                //validate the number textboxes
                if (txtCompPhone.Text != "")
                {
                    try
                    {
                        //validate the phone number
                       // txtCompPhone.Text = Convert.ToString(Convert.ToInt32(txtCompPhone.Text));
                        try
                        {
                            if (txtCompFax.Text != "")
                            {
                                //validate the fax number
                                txtCompFax.Text = Convert.ToString(Convert.ToInt32(txtCompFax.Text));
                                if (txtCompWebSite.Text != "")
                                {
                                    //validate the website
                                    char[] web = { '@', '.' };
                                    if (txtCompWebSite.Text.IndexOfAny(web) != -1)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        MessageBox.Show("Enter a valid web site address");
                                        txtCompWebSite.Focus();
                                        return false;
                                    }
                                }
                            }
                        }
                        catch (Exception e1)
                        {
                            MessageBox.Show("Enter a valid Fax No");
                            txtCompFax.Focus();
                            return false;
                        }
                    }
                    catch (Exception e1)
                    {
                        MessageBox.Show("Enter a valid Phone No");
                        txtCompPhone.Focus();
                        return false;
                    }
                }
            }
            return true;
        }

        // Function name   : ClearControls
        // Description     : to clear the controls
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        public void ClearControls()
        {
            txtCompId.Text = "";
            txtCompName.Text = "";
            txtCompAddress.Text = "";
            txtCompCity.Text = "";
            txtCompState.Text = "";
            txtCompPhone.Text = "";
            txtCompWebSite.Text ="";
            txtCompFax.Text = "";
            groupBox1.Visible = false;
        }

        // Function name   : btnClose_Click
        // Description     : to close the current window
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/18/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnSave_Paint(object sender, PaintEventArgs e)
        {
         //This method will change the square button to a circular button by 
         //creating a new circle-shaped GraphicsPath object and setting it 
         //to the RoundButton objects region.
            //System.Windows.Forms.Button tButton = (System.Windows.Forms.Button)sender;
			

            //System.Drawing.Drawing2D.GraphicsPath buttonPath = 
            //    new System.Drawing.Drawing2D.GraphicsPath();

			
            // //Set a new rectangle to the same size as the button's 
            // //ClientRectangle property.
            //System.Drawing.Rectangle newRectangle = tButton.ClientRectangle;
			
            // //Decrease the size of the rectangle.
            //newRectangle.Inflate(-10, -10);
        
            // //Draw the button's border.
            //e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);

            // //Increase the size of the rectangle to include the border.
            //newRectangle.Inflate( 2,  2);


            // //Create a circle within the new rectangle.
            //buttonPath.AddEllipse(newRectangle);
                
            // //Set the button's Region property to the newly created 
            // //circle region.
            //tButton.Region = new System.Drawing.Region(buttonPath);
            //tButton.Size = new Size(90, 50);
        }

        private void groupBox1_Paint(object sender, PaintEventArgs e)
       {
        //    float t = (float)0.5;
        //    Pen pen = new Pen(Color.Gray, t);

        //    Graphics g;
        //    Graphics hh;


        //    foreach (Control ctr in this.Controls)
        //    {
        //        g = ctr.CreateGraphics();
        //        if (ctr is GroupBox)
        //        {
        //            foreach (Control TextCtrl in ((GroupBox)ctr).Controls)
        //            {
        //                //check all controls in GroupBox container

        //                if (TextCtrl is TextBox)
        //                {
        //                    //determine TextBox

        //                    g.DrawRectangle(pen, new Rectangle(((TextBox)TextCtrl).Location, ((TextBox)TextCtrl).Size));
        //                }
        //            }
        //        }
        //    }
        }

          
    }
}
